package com.healthai.service;

import com.healthai.dto.request.ExerciseRecordRequest;
import com.healthai.entity.ExerciseRecord;
import com.healthai.exception.BusinessException;
import com.healthai.mapper.ExerciseRecordMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

@Service
public class ExerciseRecordService {
    
    @Autowired
    private ExerciseRecordMapper exerciseRecordMapper;
    
    /**
     * 创建运动记录
     * @param userId 用户ID
     * @param request 运动记录请求
     * @return 记录ID
     */
    public Integer createExerciseRecord(Integer userId, ExerciseRecordRequest request) {
        ExerciseRecord record = new ExerciseRecord();
        BeanUtils.copyProperties(request, record);
        record.setUserId(userId);
        record.setCreateTime(LocalDateTime.now());
        record.setUpdateTime(LocalDateTime.now());
        
        exerciseRecordMapper.insert(record);
        return record.getExerciseId();
    }
    
    /**
     * 更新运动记录
     * @param userId 用户ID
     * @param recordId 记录ID
     * @param request 运动记录请求
     * @return 是否成功
     */
    public boolean updateExerciseRecord(Integer userId, Integer recordId, ExerciseRecordRequest request) {
        // 检查记录是否存在且属于该用户
        ExerciseRecord existingRecord = exerciseRecordMapper.findById(recordId);
        if (existingRecord == null || !existingRecord.getUserId().equals(userId)) {
            throw new BusinessException(404, "记录不存在或无权限修改");
        }
        
        ExerciseRecord record = new ExerciseRecord();
        BeanUtils.copyProperties(request, record);
        record.setExerciseId(recordId);
        record.setUserId(userId);
        record.setUpdateTime(LocalDateTime.now());
        
        return exerciseRecordMapper.update(record) > 0;
    }
    
    /**
     * 获取用户某日期的运动记录
     * @param userId 用户ID
     * @param date 日期
     * @return 运动记录列表
     */
    public List<ExerciseRecord> getExerciseRecordsByDate(Integer userId, LocalDate date) {
        return exerciseRecordMapper.findByUserIdAndDate(userId, date);
    }
    
    /**
     * 获取用户日期范围内的运动记录
     * @param userId 用户ID
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 运动记录列表
     */
    public List<ExerciseRecord> getExerciseRecordsByDateRange(Integer userId, LocalDate startDate, LocalDate endDate) {
        return exerciseRecordMapper.findByUserIdAndDateRange(userId, startDate, endDate);
    }
    
    /**
     * 获取用户最近的运动记录
     * @param userId 用户ID
     * @param limit 限制数量
     * @return 运动记录列表
     */
    public List<ExerciseRecord> getRecentExerciseRecords(Integer userId, Integer limit) {
        return exerciseRecordMapper.findRecentByUserId(userId, limit);
    }
    
    /**
     * 删除运动记录
     * @param userId 用户ID
     * @param recordId 记录ID
     * @return 是否成功
     */
    public boolean deleteExerciseRecord(Integer userId, Integer recordId) {
        // 检查记录是否存在且属于该用户
        ExerciseRecord record = exerciseRecordMapper.findById(recordId);
        if (record == null || !record.getUserId().equals(userId)) {
            throw new BusinessException(404, "记录不存在或无权限删除");
        }
        
        return exerciseRecordMapper.deleteById(recordId) > 0;
    }
}